﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using TianYa.API.Response;
using TianYa.API.Request;
using TianYa.Data;

namespace TianYa.View
{
    public partial class PlatePage : GBasePage
    {
        public PlatePage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //string name = typeof(Test1).Name;
            list_fav.ItemsSource = GloabalData.Instance.TianYaDataBase.FavoritPlateTable.ToList();

            //List<object> data = new List<object>();
            //data.Add(new Test1() { Name = "test1" });
            //data.Add(new Test2() { Name = "test2" });
            //list.ItemsSource = data;
        }

        private void longList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (longList.SelectedItem != null)
            {
                API.Domain.PlateInfo plateinfo = longList.SelectedItem as API.Domain.PlateInfo;

                this.NavigationService.Navigate(string.Format("/View/PlatePostListPage.xaml?plateid={0}&plateName={1}", plateinfo.Id, plateinfo.Name));

            }
        }

        private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (pivot.SelectedIndex == 1)
            {
                if (this.longList.ItemsSource == null)
                {
                    this.RegisterLoading("正在加载所有板块列表...");
                    GloabalData.Instance.APIClient.Execute<GetAllPlateResponse>(new GetAllPlateRequest(), (c) =>
                    {
                        var aa = from row in c.PlateInfos
                                 group row by row.Category into groupRows
                                 orderby groupRows.Key
                                 select new PublicGrouping<string, API.Domain.PlateInfo>(groupRows);
                        this.Dispatcher.BeginInvoke(() =>
                        {
                            this.longList.ItemsSource = aa;
                            this.UnregisterLoading();
                        });
                    });
                }
            }
        }

        private void list_fav_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (list_fav.SelectedItem != null)
            {
                Data.FavoritPlate f = list_fav.SelectedItem as Data.FavoritPlate;

                this.NavigationService.Navigate(string.Format("/View/PlatePostListPage.xaml?plateid={0}&plateName={1}", f.PlateId, f.Name));
                list_fav.SelectedItem = null;
            }
        }
    }
}